package com.jinlejia.erp.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;
import com.weight.CustomToast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author SongJiaTai
 * @description:
 * @date :2020/3/18 12:57
 */
public class EditShareDialog extends AlertDialog {
    public EditShareDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_editshare);
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        //立即分享
        TextView tv_share = window.findViewById(R.id.tv_share);

        EditText et_sellprice = window.findViewById(R.id.et_sellprice);

        LinearLayout bt_clear = window.findViewById(R.id.bt_clear);

        et_sellprice.setFocusable(true);
        et_sellprice.setFocusableInTouchMode(true);
        et_sellprice.requestFocus();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            }
        }, 220);

        et_sellprice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bt_clear.setVisibility(View.VISIBLE);
                } else {
                    bt_clear.setVisibility(View.INVISIBLE);
                }
            }
        });
        bt_clear.setOnClickListener(v -> {
            et_sellprice.setText("");
        });


        tv_share.setOnClickListener(v -> {
            //确定
            if (!TextUtils.isEmpty(et_sellprice.getText())) {
                if (Integer.parseInt(et_sellprice.getText().toString()) == 0) {
                    CustomToast.showToast(getContext(), "输入价格不能为0", 1500);
                } else {
                    dismiss();
                    if (null != confirmListener) {
                        confirmListener.confirm(et_sellprice.getText().toString());
                    }
                }
            } else {
                CustomToast.showToast(getContext(), "请先输入价格", 1500);
            }

        });
    }

    public interface OnConfirmListener {
        void confirm(String price);
    }

    private OnConfirmListener confirmListener;

    public void setConfirmListener(OnConfirmListener listener) {
        this.confirmListener = listener;
    }

}
